package com.ykzb.pay.weixinpay;

import org.jdom.JDOMException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Created by Administrator on 2016/6/15.
 */
public class WeiXinPayManager {

    public static WeiXinPayManager manager;

    public static WeiXinPayManager getManager()
    {
        if (manager == null);
        {
            manager = new WeiXinPayManager();
        }
        return manager;
    }
    //1 NATIVE 2 JSAPI
    public Map getQrcodeURL(String money, String no , String url , int type,String openid)
    {
        Map map = new HashMap<String,Object>();
        String  nonce_str = PayCommonUtil.CreateNoncestr();

        SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
        parameters.put("appid", ConfigUtil.APPID);
        parameters.put("mch_id", ConfigUtil.MCH_ID);
        parameters.put("nonce_str",nonce_str );
        parameters.put("body", "黑猫云车");
        parameters.put("out_trade_no", no);
        parameters.put("total_fee", money);
        parameters.put("spbill_create_ip","120.25.60.198");
        parameters.put("notify_url", url);
        if (type == 1)
        {
            parameters.put("product_id", no);
            parameters.put("trade_type", "NATIVE");
        }
        if (type == 2)
        {
            parameters.put("openid", openid);
            parameters.put("trade_type", "JSAPI");

        }

        String sign = PayCommonUtil.createSign("UTF-8", parameters);
        parameters.put("sign", sign);
        String requestXML = PayCommonUtil.getRequestXml(parameters);
        String result =CommonUtil.httpsRequest(ConfigUtil.UNIFIED_ORDER_URL, "POST", requestXML);
        try {
            map = XMLUtil.doXMLParse(result);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

}
